import 'package:dio/dio.dart';
import 'package:flutter_app/httpServe/httpServe.dart';
class DioHttp{
  static DioHttp _instance;
  HttpServe httpServe = HttpServe();
  DioHttp._internal([selfOptions]) {
    httpServe.dio.interceptors.add(InterceptorsWrapper(
        onRequest:(RequestOptions options) async {
          if(selfOptions == null){
            options = selfOptions;
          }
          return options; //continue
        },
        onResponse:(Response response) async {
          if (response.statusCode == 200) {
            return Future.value(response.data);
          } else {
            return Future.error(response.data);
          }
        },
        onError: (DioError e) async {
          return  Future.error(e);//continue
        }
    ));
  }
  static DioHttp _getInstance([selfOptions]) {
    if (_instance == null) {
      _instance = DioHttp._internal(selfOptions);
    }
    return _instance;
  }
  factory DioHttp([selfOptions]){
    return DioHttp._getInstance(selfOptions);
  }
  Future get(url,[data]){
    return httpServe.get(url,data);
  }
  Future post(url,[data]){
    httpServe.dio.options.headers['content-type'] = "application/x-www-form-urlencoded";
    return httpServe.post(url,data);
  }

  Future downFile(url,name){
    return httpServe.downFile(url,name);
  }
  Future uploadFile(url,formData,[onSendProgress]){
    httpServe.dio.options.headers['content-type'] = 'multipart/form-data';
    return httpServe.uploadFile(url,formData,onSendProgress);
  }
}